package cn.ylz.ylmall.shopping.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingParam;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingQuantityParam;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingSelectedAllParam;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingSelectedParam;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO1;
import cn.ylz.ylmall.shopping.service.IShoppingService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 圈子模块
 */
@Slf4j
@RestController
@RequestMapping("/v1/shopping")
@Validated
@Api(tags = "4.1. 购物车模块")

public class ShoppingController {
    @Autowired
    private IShoppingService shoppingService;



    @PostMapping("/post-addCart")
    @ApiOperation("加入购物车")
    @ApiOperationSupport(order = 401)
    public JsonResult postAddCart(@AuthenticationPrincipal  CurrentPrincipal currentPrincipal,
                                  @RequestBody ShoppingParam shoppingParam){
        log.debug("开始处理【加入购物车】的请求，参数：{}", shoppingParam);
        shoppingService.addNew(shoppingParam, currentPrincipal.getId());
        return JsonResult.ok();
    }
    @GetMapping("/get-showCart")
    @ApiOperation("显示购物车")
    @ApiOperationSupport(order = 402)
    public JsonResult postShowCart(@AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        Long userId = currentPrincipal.getId();
        log.debug("开始处理【显示购物车】的请求，参数：{}", userId);
        List<ShoppingVO1> list = shoppingService.showAll(userId);
        return JsonResult.ok(list);
    }

    @PostMapping("/post-updateCart")
    @ApiOperation("更改商品数量和选中状态")
    @ApiOperationSupport(order = 403)
    public JsonResult postUpdateCart(@RequestBody ArrayList<ShoppingQuantityParam> lis){
        log.debug("开始处理【更改商品数量和选中状态】的请求，参数：{}", lis);
        shoppingService.updateQuantity(lis);
        return JsonResult.ok();
    }

    @PostMapping("/post-deleteCart")
    @ApiOperation("删除选中商品")
    @ApiOperationSupport(order = 404)
    public JsonResult postSelectedCart(@RequestBody ArrayList<Long> list){

        log.debug("开始处理【删除选中商品】的请求，参数：{}", list);
        shoppingService.delete(list);
        return JsonResult.ok();
    }

    @PostMapping("/post-selectCartId")
    @ApiOperation("根据cartId查商品")
    @ApiOperationSupport(order = 405)
    public JsonResult postAddCart(@RequestBody ArrayList<Long> list){
        log.debug("开始处理【根据cartId查商品】的请求，参数：{}", list);
        List<ShoppingVO1> arr = shoppingService.selectCartId(list);
        return JsonResult.ok(arr);
    }




//    @PostMapping("/post-showDeclineCart")
//    @ApiOperation("显示降价商品")
//    @ApiOperationSupport(order = 405)
//    public JsonResult postShowDeclineCart(@ApiIgnore HttpServletRequest request, @RequestParam @Valid Long userId){
//        log.debug("开始处理【显示降价商品】的请求，参数：{}", userId);
//        List<ShoppingVO> list = shoppingService.showDeclineAll(userId);
//        return JsonResult.ok(list);
//    }
//
//    @PostMapping("/post-showHavingCart")
//    @ApiOperation("显示常买商品")
//    @ApiOperationSupport(order = 406)
//    public JsonResult postShowHavingCart(@ApiIgnore HttpServletRequest request, @RequestParam @Valid Long userId){
//        log.debug("开始处理【显示常买商品】的请求，参数：{}", userId);
//        List<ShoppingVO> list = shoppingService.showHavingAll(userId);
//        return JsonResult.ok(list);
//    }



}
